#coding=utf-8
from pymongo import MongoClient
from bson import json_util
import bson
import time
import random
import socket
from random import randint
from static.configfile.config import *
url = mongo_url_to_db
def find_task(item):
    mongo_url_to_db = url  # 传入DB
    client =MongoClient(mongo_url_to_db)
    colection =client["Task"]["task"]
    return colection.find_one(item)


def update_task(item,update):
    mongo_url_to_db = url  # 传入DB
    client =MongoClient(mongo_url_to_db)
    colection =client["Task"]["task"]
    update["savetime"] = time.strftime("%Y-%m-%d",time.localtime())
    colection.update_one(item, {"$set": update})

def insert_task(item):
    item["savetime"] = time.strftime("%Y-%m-%d",time.localtime())
    item["_id"] = item["host"]+"."+str(bson.int64.Int64(time.time()))
    mongo_url_to_db = url  # 传入DB
    client =MongoClient(mongo_url_to_db)
    colection =client["Task"]["task"]
    colection.insert_one(item)

def insert_data(item):
    mongo_url_to_db = url  # 传入DB
    client =MongoClient(mongo_url_to_db)
    colection =client["Task"]["doc_xiaozhan"]
    colection.insert_one(item)



if __name__ == "__main__":
    data = []
    # pingtai = ["微信","微博","网媒"]
    pingtai = ["微信","微博API","微博Scrapy","百家号"]
    status = ["stop","wait","run"]
    for i in range(50,100):
        item = {}
        item["name"] = str(i)+"测试na哈哈"
        # item["kwd"] = "ncjds mds"
        item["source"] = pingtai[randint(0,2)]
        item["host"]  = "127.0.0.1"
        item["process"] = "34556"
        # item["fromdate"] = "2011-07-01"
        # item["todate"] = "2019-11-23"
        item["complete"]=random.randint(10000,100000)
        item["savetime"] = time.strftime("%Y-%m-%d",time.localtime())
        # item["_id"] = "127.0.0.1."+str(item["savetime"])
        item["cycle"] = "23hour" #记录执行任务的周期
        item["cyclic"] = 1 #1表示需要周期性执行，0表示不需要，默认需要周期性执行
        item["status"] = status[randint(0,2)]
        cookies = '''
        SINAGLOBAL=4228516345142.5146.1636452084535; UOR=,,login.sina.com.cn; SSOLoginState=1645063085; _s_tentry=login.sina.com.cn; Apache=7301222153829.403.1645063086618; ULV=1645063086704:42:3:2:7301222153829.403.1645063086618:1644979576624; XSRF-TOKEN=1085DyFDfAHGZrHRvWT5fWvu; wvr=6; wb_view_log_7580754358=2560*14401.5; webim_unReadCount={"time":1645147837393,"dm_pub_total":5,"chat_group_client":0,"chat_group_notice":0,"allcountNum":43,"msgbox":0}; SUBP=0033WrSXqPxfM725Ws9jqgMF55529P9D9WhP.FkzJP1O-12.ZLUWcM925JpX5KMhUgL.FoMf1h5NSKB0SKn2dJLoIpnLxK.L1KnLB.qLxKnLBoBLBK8keoe0eK5t; ALF=1676685587; SCF=AiSyNpSzC54tCLxTtaviN8v-vOyXWBjTccCICzVyFVxNBOGwF7QGJ-JutmqEPaMDp7ASxZQgqNg2GNgbMRDYq8w.; SUB=_2A25PCo3DDeRhGeFL41IW9SrPzjSIHXVsYfgLrDV8PUNbmtB-LRH9kW9NfbZkGUjGSVteRYiStUeSP0m2BPOwxXt_; WBPSESS=Bwe7TAq2cW8qQzz2eO0k5LlKzg7GzYS8wTxVoXfBBEUHxYWOJEKTaIeqmjY6g__1kPJrBYhSvA3OzqLDhC1DKpH9c_jO5fpOkV-fDCLq1IYiirvnjTv-B7V9_q6KKQkRnB71vWhEBReV5ipeKhO0OA==
        '''
        item["canshu"] = {"kwd": "123", "fromdate": "2021-12-01", "todate": "2021-12-04", "cookies": cookies,"dbname":"doc_{}".format(str(i))}
        # item["cookies"] = "dvaburghwiervw1468s7drfvwAIHF47HFUWnevfv8s7dtkhbks"
        insert_task(item)
        time.sleep(1)
    # date=["2021-12-10","2021-12-11","2021-12-12","2021-12-13","2021-12-14"]
    # date = [1642052948,1641880148,1610344148,1636645748,1620748161,1621612161]

    # date = [1642089600,1642003200,1641916800,1641830400,1641916799,1642003199,1642089599,1642175999]
    # for i in range(1,120):
    #     item = {}
    #     item["id"] = i
    #     item["author"] = "肖战后援会"
    #     item["source"] = "微博sdjcpos号"
    #     item["pubTime"] = date[randint(0, 7)]
    #     item["retweetNumber"] = 100000
    #     item["reviewNumber"] = 100000
    #     item["authorLink"] = "https://www.bilibili.com/"
    #     item["channel"] = pingtai[randint(0,3)]
    #     item["mainText"] = "123456cndusdhn1cao表示需要周期性执行，0表示不需要，默认需要周期性执行djkbnaOSDHNHOVASDV5465"
    #     # data.append(item)
    #     insert_data(item)
    